package com.his.config;


import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;


@Configuration
public class ShiroConfig {


    /**
     * 得到JwtFilter实例对象
     * @return
     */
    @Bean(name = "jwtFilter")
    public JwtFilter jwtFilter(){
        return new JwtFilter();
    }

    /**
     * 得到自定义的Realm
     *
     * @return
     */
    @Bean
    public Realm realms() {
        MyRealm myRealm = new MyRealm();
        return myRealm;
    }






    @Bean
    public ShiroFilterChainDefinition definition(){
        ShiroFilterChainDefinition shiroFilterChainDefinition = new DefaultShiroFilterChainDefinition();
        Map<String, String> filterChainMap = shiroFilterChainDefinition.getFilterChainMap();
//        filterChainMap.put("/login.html","anon");
        filterChainMap.put("/dzm/his/member/doLogin","anon");
//        filterChainMap.put("/index.html","anon");
//        filterChainMap.put("/jquery-1.12.4.js","anon");
        filterChainMap.put("/queryAuthc", "anon");

//        filterChainMap.put("/doSomething","anon");
        // 在header中加入新字段，只适合于AJAX请求，不适合于普通的跳转等
        // 因为我们采用了JWT，并且采用在header中加入Authorization的方式来校验
        // 我们现在没有办法对静态html资源做认证
        // 我们可以改变我们的思路，在success.html中做AJAX请求，然后判断是否登录
//        filterChainMap.put("/success.html", "anon");

//        授权
//        filterChainMap.put("/add.html","perms[user:add]");
//        filterChainMap.put("/update.html","perms[user:update]");

        filterChainMap.put("/**","jwtFilter");
        return shiroFilterChainDefinition;
    }






    @Bean
    public FilterRegistrationBean<JwtFilter> jwtFilterRegistration() {
        FilterRegistrationBean<JwtFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(jwtFilter());
        // 让我们的JwtFilter不要在SpringBoot中直接生效
        // 以防它拦截了我们不想让它拦截的请求
        filterRegistrationBean.setEnabled(false);
        return filterRegistrationBean;
    }

//    @Bean
//    public SessionStorageEvaluator sessionStorageEvaluator() {
//        DefaultSessionStorageEvaluator sessionStorageEvaluator = new DefaultSessionStorageEvaluator();
//        // 告诉Shiro不要尝试生成Session
//        sessionStorageEvaluator.setSessionStorageEnabled(false);
//        return sessionStorageEvaluator;
//    }


}
